#include "BuildingPlacerImpl.h"

#include "..\BWAPI\Unit.h"
#include "BFSBuildingPlacer.h"
#include "ReservedMap.h"

using namespace BroodWar::Api;

namespace BroodWar
{
	namespace Addon
	{
		BWSAL::BuildingPlacer* GetBuildingPlacer(IBuildingPlacer^ placer)
		{
			BFSBuildingPlacer^ bfs = dynamic_cast<BFSBuildingPlacer^>(placer);
			if(bfs != nullptr)
				return (BWSAL::BuildingPlacer*)(bfs->instance);
			#pragma warning memory leak!
			return (BWSAL::BuildingPlacer*)(new BuildingPlacerImpl(placer));
		}

		BuildingPlacerImpl::BuildingPlacerImpl(IBuildingPlacer^ handle)
		{
			_handle = handle;
		}

		BWAPI::TilePosition BuildingPlacerImpl::findBuildLocation(
			BWSAL::ReservedMap* reserveMap,
			BWAPI::UnitType unitType,
			BWAPI::TilePosition seedLocation,
			BWAPI::Unit* builder)
		{
			if(builder == NULL)
				return ConvertTile(_handle->FindBuildLocation(ConvertReservedMap(reserveMap), ConvertUnitType(unitType)->Type, ConvertTile(seedLocation)));
			return ConvertTile(_handle->FindBuildLocation(ConvertReservedMap(reserveMap), ConvertUnitType(unitType)->Type, ConvertTile(seedLocation), ConvertUnit(builder)));
		}
	}
}
